/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package digitaldreamers.libraries.games.servers.networkmanagement.packets.sending;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;

/**
 * Abstract class for all sendable packet classes.
 *
 * @author DaoLe
 */
public abstract class AbSendablePacket {
    // Packet ID.
    protected int mPacketId;

    /**
     * Convert packet data to byte array.
     *
     * @return The converted byte array.
     * @throws IOException Error that occurred while converting data.
     */
    public byte[] toByteArray() throws IOException {
        ByteArrayOutputStream BAOS = new ByteArrayOutputStream();
        DataOutputStream DOS = new DataOutputStream(BAOS);
        try {
            writeData(DOS);
            byte[] arr_byData = BAOS.toByteArray();
            return arr_byData;
        } catch (IOException ex) {
            throw ex;
        } finally {
            DOS.close();
            BAOS = null;
            DOS = null;
        }
    }
    
    /**
     * Write packet data.
     * 
     * @param pDOS Output stream
     * @throws IOException Error that occurred while writing data.
     */
    protected abstract void writeData(DataOutputStream pDOS) throws IOException;
}